//
//  BOrderMallRootVC.m
//  DistributorAB
//
//  Created by LZY on 2019/9/29.
//  Copyright © 2019 主事丫环. All rights reserved.
//

// B级经销商产品展示RootVC

#import "BOrderMallRootVC.h"
#import "HWScanViewController.h"//扫码申请补助金
#import "BApplyForMoneyVC.h"
#import "BBuyNowVC.h"//我要预订
#import "ZMyOrderCarVC.h"//预订车
#import "ZMyOrderListVC.h"//预订单
#import "ZMyOrderManagerVC.h"//预订管理
#import "ZYHouBaoOrderFormViewController.h"
#import "ShiXianJiZGrantListSubAllOrdersViewController.h"
#import "ZZBuyNowShopHeaderView.h"
#import "BBuyNowShopCell.h"
#import "BShopPartModel.h"
#import "BBuyNowShopDetailVC.h"//产品详情

@interface BOrderMallRootVC () <UITableViewDelegate, UITableViewDataSource, ZBuyNowShopHeaderViewDeletate>
{
    BOOL _isRequesting;//是否正在请求数据
}
@property (nonatomic, strong) NSMutableArray *bannerListArray;//轮播图数据
@property (nonatomic, strong) UICollectionView *viewCollection;//块布局
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *shopList;//产品列表
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) NSInteger index;

@end

@implementation BOrderMallRootVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _shopList = [NSMutableArray array];

    [self setupNavViewAction];
    [self setupViewAction];
    
    [self setupViewAction];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    //状态栏字体白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    //状态栏字体黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark - 自定义导航栏
- (void)setupNavViewAction{
    UIView *viewNav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kNaviHeight)];
    [self.view addSubview:viewNav];
    
    UIImageView *ivBg = [[UIImageView alloc] initWithFrame:viewNav.bounds];
    ivBg.image = imgname(@"base_nav_bg");
    [viewNav addSubview:ivBg];
    
    UILabel *lblNav = [[UILabel alloc] initWithFrame:CGRectMake(0, kNaviHeight - 44, kScreen_Width, 44)];
    lblNav.text = @"产品展示";
    lblNav.font = [UIFont systemFontOfSize:18];
    lblNav.textColor = kColorWithHex(0xffffff);
    lblNav.textAlignment = NSTextAlignmentCenter;
    [viewNav addSubview:lblNav];
}

#pragma mark - 界面布局方法
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 300;
    
    [_tableView registerClass:[BBuyNowShopCell class] forCellReuseIdentifier:NSStringFromClass([BBuyNowShopCell class])];
    
    ZZBuyNowShopHeaderView *viewHeader = [[ZZBuyNowShopHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScaleNum(122) * 3 + 15)];
    viewHeader.delegate = self;
    _tableView.tableHeaderView = viewHeader;
    
    kWeakSelf;
    //下拉刷新
    MJRefreshNormalHeader *mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageNumber = 1;
        [weakSelf requestData];
    }];
    mj_header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.mj_header = mj_header;
    
    //上拉加载
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestData];
    }];
    
    [_tableView.mj_header beginRefreshing];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shopList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BBuyNowShopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BBuyNowShopCell class]) forIndexPath:indexPath];
    BShopPartModel *model = [self.shopList objectAtIndexSafe:indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BShopPartModel *model = [self.shopList objectAtIndexSafe:indexPath.row];
    BBuyNowShopDetailVC *vc = [BBuyNowShopDetailVC new];
    vc.shopID = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - ZBuyNowShopHeaderViewDeletate
- (void)buyNowShopHeaderViewClick:(ZBuyNowShopHeaderViewCategoryType)type{
    
    NSString *str = [USER_DEFAULT objectForKey:@"is_power"];
    if ([str isEqualToString:@"0"]) {
        ShowToastWithText(@"请到个人信息激活管理员身份");
        return;
    }
    
    if (type == ZBuyNowShopHeaderViewCategoryTypeSmsq) {
        //扫码申请
        HWScanViewController *vc = [[HWScanViewController alloc]init];
        vc.myblock = ^(NSString *str) {
            BApplyForMoneyVC *vc = [BApplyForMoneyVC new];
            vc.codeStr = str;
            [self.navigationController pushViewController:vc animated:YES];
        };
        UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:vc];
        [self presentViewController:na animated:YES completion:nil];
    } else if (type == ZBuyNowShopHeaderViewCategoryTypeLjyd){
        //立即预订
        BBuyNowVC *vc = [BBuyNowVC new];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (type == ZBuyNowShopHeaderViewCategoryTypeYdml){
        //预订目录  2个界面合并，使用ZYHouBaoOrderFormViewController，替代了之前的ZMyOrderCarVC和ZMyOrderListVC
        ZYHouBaoOrderFormViewController *vc = [ZYHouBaoOrderFormViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    } else if (type == ZBuyNowShopHeaderViewCategoryTypeSqbzj){
        //市县级 只显示补助金申请 全部数据列表
        ShiXianJiZGrantListSubAllOrdersViewController *vc = [ShiXianJiZGrantListSubAllOrdersViewController new];
        vc.type = ZGrantListSubVCTypeAll;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (type == ZBuyNowShopHeaderViewCategoryTypeYdgl){
        //预订管理
        ZMyOrderManagerVC *vc = [ZMyOrderManagerVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - 数据请求
- (void)requestData{
    if (_isRequesting) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        return;
    }
    _isRequesting = YES;
    [MBProgressHUD showHUDAddedTo:self.view msg:@"数据加载中" animated:YES];
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObjectIfNotNil:@"1" forKey:@"type"];
    [param setObject:@(_pageNumber) forKey:@"pageNum"];
    [NetWorkTool POST:getPartsList param:param success:^(id dic) {
        [weakSelf requestResult];
        
        NSArray *dataArr = [dic objectForKeyNotNil:@"data"];
        [dataArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BShopPartModel *model = [BShopPartModel mj_objectWithKeyValues:obj];
            [weakSelf.shopList addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
        weakSelf.pageNumber++;
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [weakSelf requestResult];
    } fail:^(NSError *error) {
        [weakSelf requestResult];
    } needUser:YES];
}
- (void)requestResult{
    _isRequesting = NO;
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if (self.pageNumber == 1) {
        [self.shopList removeAllObjects];
    }
}

@end
